<?php

namespace App\Http\Controllers\Pay\Order;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Common\DataResource;
use App\Http\Resources\Common\DatasResource;
use App\Models\Game\Pay\Order\Wallet as Order;
use App\Services\Pay\Order\WalletService;
use App\Services\Postman\GameService as PostmanGameService;
use App\Exceptions\Response\ServiceException as ResponseServiceException;

class WalletController extends Controller
{

    /**
     * 导出数据
     */
    public function export(Request $request)
    {
        WalletService::create()->getList($request, true);
    }

    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total, $stats) = WalletService::create()->getList($request);

        return DatasResource::create($collection)->setPaginateTotal($total)->setAttribute('stats', $stats);
    }

    /**
     * 订单取消
     */
    public function orderCancel(Request $request)
    {
        $data = $request->validate([
            'sn' => 'required|string|max:128',
            'plat_code' => 'required|string|max:64',
        ]);
        // 取消订单
        PostmanGameService::post(PostmanGameService::URI_WALLET_TIMELY_CANCEL, $data);
        // 获取订单
        if (is_null($order = Order::getData($data['sn'], $data['plat_code']))) {
            throw new ResponseServiceException(ResponseServiceException::SERVICE_NOT_EXIST_DATA);
        }

        return new DataResource($order);
    }
}
